﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LogBase
{
    public class DataManage
    {
        /// <summary>
        /// 迁移分析数据到历史表
        /// </summary>
        /// <param name="Date">备份的日期点，该日期前的数据会进行迁移到历史表</param>
        /// <returns>int</returns>
        public static int MoveRecord(string sitename,string Date)
        {
            string sql = "";
            int rval = 0;
            List<string> tables = new List<string>(new string[3] { "QT_AnalybyIP", "QT_AnalybyPage", "QT_AnalybyGeneral" });
            foreach (string table in tables)
            {
                sql = "insert into [" + table + "_History] select * from [" + table + "] where [date]<='" + Date + "'" + (sitename == null ? "" : " and [SiteName]='" + sitename + "'");
                if (new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql) == -1) rval = -1;
            }
            foreach (string table in tables)
            {
                sql = "delete from [" + table + "] where [date]<='" + Date + "'" + (sitename == null ? "" : " and [SiteName]='" + sitename + "'");
                if (new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql) == -1) rval = -1;
            }
            return rval;
        }

        /// <summary>
        /// 删除单个日期的分析结果
        /// </summary>
        /// <param name="sitename">站点ID。null表示所有站点</param>
        /// <param name="Date">日期</param>
        /// <returns>int</returns>
        public static int DeleteRecord(string sitename, string Date)
        {
            string sql = "";
            int rval = 0;
            List<string> tables = new List<string>(new string[3] { "QT_AnalybyIP", "QT_AnalybyPage", "QT_AnalybyGeneral" });
            foreach (string table in tables)
            {
                sql = "delete from [" + table + "] where [date]='" + Date + "'" + (sitename == null ? "" : " and [SiteName]='" + sitename + "'");
                if (new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql) == -1) rval = -1;
            }
            return rval;
        }

        /// <summary>
        /// 删除一段日期的分析结果
        /// </summary>
        /// <param name="sitename">站点ID。null表示所有站点</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>int</returns>
        public static int DeleteMultiRecord(string sitename, string startDate, string endDate)
        {
            string sql = "";
            int rval = 0;
            List<string> tables = new List<string>(new string[3] { "QT_AnalybyIP", "QT_AnalybyPage", "QT_AnalybyGeneral" });
            foreach (string table in tables)
            {
                sql = "delete from [" + table + "] where [date]>='" + startDate + "' and [date]<='" + endDate + "'" + (sitename == null ? "" : " and [SiteName]='" + sitename + "'");
                if (new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql) == -1) rval = -1;
            }
            return rval;
        }
    }
}
